//
//  SAHomeViewController.m
//  SAP
//
//  Created by 中矿科技有限公司 on 2017/11/9.
//  Copyright © 2017年 中矿科技有限公司. All rights reserved.
//

#import "SAHomeViewController.h"
#import "WorkBenchModel.h"
#import "GraphView.h"

@interface SAHomeViewController ()<ChartViewDelegate>
@property (weak, nonatomic) IBOutlet GraphView *myGraph;
@property (weak, nonatomic) IBOutlet UIButton *contractBtn;
@property (weak, nonatomic) IBOutlet UIButton *transfersBtn;



@end

@implementation SAHomeViewController
{
    AFHTTPData *_http;
    WorkBenchModel * _workBenchmodel;
}
#pragma mark - LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _http =[[AFHTTPData alloc]init];
    _workBenchmodel =[[WorkBenchModel alloc]init];
    [self prepareData:NO];
}




-(void)prepareData:(BOOL)update{
    [_http getDataWithUrl:SAPHomeListKUrl onSuccess:^(NSData *data) {
      //  NSDictionary *dict =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
      //  NSLog(@"%@",dict);
        _workBenchmodel =[WorkBenchModel modelsFromJsonData:data];
        [self customUI];
    } onFailure:^(NSError *error) {
        
    }];
}


-(void)customUI{

    self.myContractToDo.text =[NSString stringWithFormat:@"%@条未读",_workBenchmodel.myContractToDo];
    self.myContractToDo_icon.text =_workBenchmodel.myContractToDo.description;
    
    self.myTransferToDo.text =[NSString stringWithFormat:@"%@条未读",_workBenchmodel.myTransferToDo];
    self.myTransferToDo_icon.text =_workBenchmodel.myTransferToDo.description;
    //判断当前未读数
    if (_workBenchmodel.myContractToDo.integerValue == 0) {
        self.myContractToDo_icon.hidden =YES;
    }
    if (_workBenchmodel.myTransferToDo.integerValue == 0) {
        self.myTransferToDo_icon.hidden =YES;
    }
    
    self.yearContractTotalCount.text =[NSString stringWithFormat:@"%.1f",_workBenchmodel.yearContractTotalCount.floatValue];
    
    self.yearTransferTotalCount.text =[NSString stringWithFormat:@"%.1f",_workBenchmodel.yearTransferTotalCount.floatValue];
    
     self.yearContractTransfer.text =[NSString stringWithFormat:@"兑现率: %@",_workBenchmodel.yearContractTransfer];
    self.progressView.progress = [_workBenchmodel.yearContractTransfer substringToIndex: _workBenchmodel.yearContractTransfer.length -1].floatValue/100;
    self.progressView.cLabel.text =@"";
    
    NSMutableArray *xTitles = [NSMutableArray array];
    for (VarietyTransferModel *model in _workBenchmodel.yearVarietyContractCount) {
        [xTitles addObject:model.key];
    }
    NSMutableArray *actualBarArray = [NSMutableArray array];
    for (VarietyTransferModel *model in _workBenchmodel.yearVarietyContractCount) {
        [actualBarArray addObject:model.value.description];
    }
    self.myGraph.xTitleArray = xTitles;
    self.myGraph.actualBarValues = actualBarArray;
    self.myGraph.xScaleLength = (SCREEN_WIDTH-30)/actualBarArray.count;
    [self.myGraph drawGraph];
}
- (IBAction)onClickedTransfersBtn:(UIButton *)sender {
    sender.selected =YES;
    self.contractBtn.selected =NO;
    NSMutableArray *xTitles = [NSMutableArray array];
    for (VarietyTransferModel *model in _workBenchmodel.yearVarietyTransferCount) {
        [xTitles addObject:model.key];
    }
    NSMutableArray *actualBarArray = [NSMutableArray array];
    for (VarietyTransferModel *model in _workBenchmodel.yearVarietyTransferCount) {
        [actualBarArray addObject:model.value.description];
    }
    self.myGraph.actualBarValues = actualBarArray;
    self.myGraph.xTitleArray = xTitles;
    self.myGraph.xScaleLength =  (SCREEN_WIDTH-30)/actualBarArray.count;
    [self.myGraph drawGraph];
    
}
- (IBAction)onClickedContractBtn:(UIButton *)sender {
    sender.selected =YES;
    self.transfersBtn.selected =NO;
    NSMutableArray *xTitles = [NSMutableArray array];
    for (VarietyTransferModel *model in _workBenchmodel.yearVarietyContractCount) {
        [xTitles addObject:model.key];
    }
    NSMutableArray *actualBarArray = [NSMutableArray array];
    for (VarietyTransferModel *model in _workBenchmodel.yearVarietyContractCount) {
        [actualBarArray addObject:model.value.description];
    }
    self.myGraph.actualBarValues = actualBarArray;
    self.myGraph.xTitleArray = xTitles;
    self.myGraph.xScaleLength =  (SCREEN_WIDTH-30)/actualBarArray.count;
    [self.myGraph drawGraph];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - UITableViewDelegate

#pragma mark - Public

#pragma mark - Private

#pragma mark - Getter

#pragma mark - Setter

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
